package com.xuxin.nantai.activity.chou;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.afollestad.materialdialogs.MaterialDialog;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.xuxin.nantai.R;
import com.xuxin.nantai.base.BaseActivity;
import com.xuxin.nantai.base.BaseConstant;
import com.xuxin.nantai.bean.BaseBean;
import com.xuxin.nantai.bean.SelectInspectorBean;
import com.xuxin.nantai.contract.SelectDetailUpdateContract;
import com.xuxin.nantai.presenter.SelectDetailUpdatePresenter;
import com.xuxin.nantai.utils.ACache;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by xu_xin
 * Created on 2020.06.17 0017
 * 缝制抽检明细新增修改
 */
public class SelectDetailUpdateActivity extends BaseActivity implements SelectDetailUpdateContract.View {
    private SelectDetailUpdateContract.Presenter presenter = new SelectDetailUpdatePresenter(this);
    @BindView(R.id.title_left)
    ImageView title_left;
    @BindView(R.id.title_right)
    ImageView title_right;
    @BindView(R.id.title_name)
    TextView title_name;

    @BindView(R.id.select_detail_update_inspector)
    TextView select_detail_update_inspector;//检验员
    @BindView(R.id.select_detail_update_defect)
    TextView select_detail_update_defect;//疵点数
    @BindView(R.id.select_detail_update_desc)
    TextView select_detail_update_desc;//疵点描述

    private String Guid;//唯一Guid(新增时为空值)
    private String DID;//唯一DID (新增时为空值)
    private String HID;//HID(对应的表头ID)
    private String ProductCode;//检验员代码
    private String ProductName;//检验员名字
    private String DefectDes;//疵点描述
    private String DefectNum;//疵点数量

    private MaterialDialog materialDialog;

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_select_detail_update);
    }

    @Override
    public void initView() {
        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            HID = bundle.getString("HID");//新增修改都需要
            if (!StringUtils.isEmpty(bundle.getString("Guid"))) {
                title_name.setText("修改明细");
                Guid = bundle.getString("Guid");
                DID = bundle.getString("DID");
                ProductName = bundle.getString("ProductName");
                ProductCode = bundle.getString("ProductCode");
                DefectDes = bundle.getString("DefectDes");
                DefectNum = bundle.getString("DefectNum");
                LogUtils.e("Guid = " + Guid);
                LogUtils.e("DID = " + DID);
                LogUtils.e("ProductName = " + ProductName);
                LogUtils.e("ProductCode = " + ProductCode);
                LogUtils.e("DefectDes = " + DefectDes);
                LogUtils.e("DefectNum = " + DefectNum);
                LogUtils.e("--------------------------");
                select_detail_update_inspector.setText(ProductName);
                select_detail_update_defect.setText(DefectNum);
                select_detail_update_desc.setText(DefectDes);
                LogUtils.e("缝制抽检_修改明细_HID = " + HID);
                LogUtils.e("缝制抽检_修改明细_Guid = " + Guid);
            } else {
                title_name.setText("新增明细");
                LogUtils.e("缝制抽检_新增明细_HID = " + HID);
                LogUtils.e("缝制抽检_新增明细_Guid = " + Guid);
            }
        }
    }

    @Override
    public void initData() {
        handler.sendEmptyMessage(0);
    }

    @Override
    public void onComplete() {
        if (null != materialDialog) {
            materialDialog.dismiss();
        }
    }

    @Override
    @OnClick({R.id.title_left, R.id.select_detail_update_inspector, R.id.select_detail_update_gain,
            R.id.complete_detail_update_update})
    public void onClick(View view) {
        KeyboardUtils.hideSoftInput(this);
        switch (view.getId()) {
            case R.id.title_left:
                finish();
                break;
            case R.id.select_detail_update_inspector:
                if (null != selectInspectorBean && null != selectInspectorBean.getData() && selectInspectorBean.getData().size() > 0) {
                    new MaterialDialog.Builder(this)
                            .title("请选择检验员")
                            .items(selectInspectorBean.getData())
                            .itemsCallback((dialog, view1, position, text) -> {
                                try {
                                    ProductName = selectInspectorBean.getData().get(position).getStaffName();
                                    ProductCode = selectInspectorBean.getData().get(position).getStaffNumber();
                                    select_detail_update_inspector.setText(ProductName);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            })
                            .show();
                }
                break;
            case R.id.select_detail_update_gain:
                try {
                    Bundle bundle_2 = new Bundle();
                    bundle_2.putString("Guid", Guid);
                    ActivityUtils.startActivityForResult(bundle_2, this,
                            DefectActivity.class, BaseConstant.result_code);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.complete_detail_update_update:
                ProductName = select_detail_update_inspector.getText().toString();
                if (StringUtils.isEmpty(ProductName)) {
                    ToastUtils.showShort("请选择检验员");
                    return;
                }
                if (StringUtils.isEmpty(DefectNum)) {
                    ToastUtils.showShort("疵点数错误");
                    return;
                }
                materialDialog = new MaterialDialog.Builder(this)
                        .content("提交数据中...")
                        .progress(true, 0)
                        .cancelable(false)
                        .show();
                handler.sendEmptyMessage(1);
                break;
        }
    }

    public Handler handler = new Handler(message -> {
        String LoginName = ACache.get(this).getAsString("LoginName");
        String UserName = ACache.get(this).getAsString("UserName");
        switch (message.what) {
            case 0://根据表头获取检验员
                presenter.getInspector(HID);
                break;
            case 1://保存缝制抽检明细
                LogUtils.e("LoginName = " + LoginName);
                LogUtils.e("UserName = " + UserName);
                LogUtils.e("Guid = " + Guid);
                LogUtils.e("DID = " + DID);
                LogUtils.e("HID = " + HID);
                LogUtils.e("ProductName = " + ProductName);
                LogUtils.e("ProductCode = " + ProductCode);
                LogUtils.e("DefectDes = " + DefectDes);
                LogUtils.e("DefectNum = " + DefectNum);
                presenter.save(LoginName, UserName, Guid, DID, HID, ProductName, ProductCode, DefectDes, DefectNum);
                break;
        }
        return false;
    });

    private SelectInspectorBean selectInspectorBean;

    @Override
    public void onSuccess(SelectInspectorBean selectInspectorBean) {
        this.selectInspectorBean = selectInspectorBean;
    }

    @Override
    public void onSuccess(BaseBean baseBean) {
        ToastUtils.showShort(baseBean.getMessage());
        setResult(BaseConstant.result_code);
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (BaseConstant.result_code == resultCode && null != data) {
            DefectDes = data.getStringExtra("DefectDes");
            DefectNum = data.getStringExtra("DefectNum");
            select_detail_update_desc.setText(DefectDes);
            select_detail_update_defect.setText(DefectNum);
        }
    }
}